# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Xin Sun <sun.simpson@gmail.com>

_realname=hdf5
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_ver=1.8.20
patch=
pkgver=${_ver}${patch//-/.}
pkgrel=1
pkgdesc="General purpose library and file format for storing scientific data (mingw-w64)"
arch=('any')
license=("custom")
url="https://www.hdfgroup.org/HDF5/"
source=("https://support.hdfgroup.org/ftp/HDF5/releases/hdf5-${_ver%.*}/hdf5-${_ver}${patch}/src/hdf5-${_ver}${patch}.tar.bz2"
        "hdf5-link-ws2_32-mingw.patch"
        "hdf5-default-import-suffix.patch"
        "hdf5-install.patch"
        "hdf5-fix-install-docs.patch"
        "hdf5-proper-library-names-mingw.patch"
        "hdf5-fix-find-szip.patch"
        "hdf5-cmake-size-type-checks.patch"
        )
sha256sums=('a4f2db7e0a078aa324f64e0216a80731731f73025367fa94d158c9b1d3fbdf6f'
            '35f8d9a313e8675e79f3920b4f2f6d349887f1b1e162dc06b1ffab9872473c3e'
            '81bd20b39ff5064cb454b7763b6cb9d3a2443c1fe0625f2d7819dce97b0b628d'
            'da0e3c30982946eddd58026359352f3cbb565e4b5ba399c8921778655c4d6505'
            '72a4c3ff11b0811e2462252ddc7f14428c0241c71b3403408dbc0f93a21271c9'
            'a21a6be1fa7e606ed16d903ea8ba670a1e387cdb9aac69e2c6468a5a9fc5c990'
            'c384db7d39123578ea7b9ead449c347ed0833bc77163c4165340a71b34baaaa0'
            '7370973e66370bb39bf2466be3ef58f464a10478b996e8c040c5a994d143d111')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"
         "${MINGW_PACKAGE_PREFIX}-szip"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-gcc-fortran"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "patch")
options=('staticlibs' 'strip')

prepare() {
  cd "${srcdir}/${_realname}-${_ver}${patch}"
  patch -p1 -i "${srcdir}/hdf5-link-ws2_32-mingw.patch"
  patch -p1 -i "${srcdir}/hdf5-install.patch"
  patch -p1 -i "${srcdir}/hdf5-default-import-suffix.patch"
  patch -p1 -i "${srcdir}/hdf5-fix-install-docs.patch"
  patch -p1 -i "${srcdir}/hdf5-proper-library-names-mingw.patch"
  #patch -p1 -i "${srcdir}/hdf5-fix-find-szip.patch"
  patch -p1 -i "${srcdir}/hdf5-cmake-size-type-checks.patch"
}

build() {
  # Build shared library
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -Wno-dev \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTING=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_SKIP_RPATH=ON \
    -DHDF5_BUILD_HL_LIB=ON \
    -DHDF5_BUILD_CPP_LIB=ON \
    -DHDF5_BUILD_FORTRAN=ON \
    -DHDF5_BUILD_TOOLS=ON \
    -DHDF5_ENABLE_DEPRECATED_SYMBOLS=ON \
    -DHDF5_ENABLE_SZIP_SUPPORT=ON \
    -DHDF5_ENABLE_Z_LIB_SUPPORT=ON \
    ../${_realname}-${_ver}${patch}

  make
}

package() {
  mkdir ${srcdir}/build-${MINGW_CHOST}/bin/static/Release/
  mkdir ${srcdir}/build-${MINGW_CHOST}/bin/shared/Release/
  cp ${srcdir}/build-${MINGW_CHOST}/bin/static/*.mod ${srcdir}/build-${MINGW_CHOST}/bin/static/Release
  cp ${srcdir}/build-${MINGW_CHOST}/bin/shared/*.mod ${srcdir}/build-${MINGW_CHOST}/bin/shared/Release
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
}
